package jp.co.nes.awssupport.common.utility;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import com.amazonaws.services.sqs.model.DeleteMessageRequest;
import com.amazonaws.services.sqs.model.DeleteMessageResult;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.amazonaws.services.sqs.model.ReceiveMessageResult;

/**
 * SQSヘルパーです。<br>
 * SQSに関する機能を提供します。
 */
public class AwsSqsHelper extends AwsHelperBase {

    /** クライアント */
    private AmazonSQS client;

    /**
     * デフォルトコンストラクタです。
     */
    public AwsSqsHelper() {
    }

    /*
     * (非 Javadoc)
     * @see jp.co.nes.awssupport.common.utility.AwsHelperBase#init(com.amazonaws.regions.Regions, com.amazonaws.auth.AWSCredentials, com.amazonaws.ClientConfiguration)
     */
    @Override
    protected void init(Regions regions, AWSCredentials credentials, ClientConfiguration config) {
        boolean isLocalTest = Boolean.valueOf(PropertiesUtil.getProperty("LocalTest", "false"));
        if (!isLocalTest) {
            if (credentials == null) {
                if (config == null) {
                    client = AmazonSQSClientBuilder.standard()
                            .withRegion(regions)
                            .build();
                } else {
                    client = AmazonSQSClientBuilder.standard()
                            .withRegion(regions)
                            .withClientConfiguration(config)
                            .build();
                }
            } else {
                if (config == null) {
                    client = AmazonSQSClientBuilder.standard()
                            .withRegion(getRegions())
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(new ClientConfiguration())
                            .build();
                } else {
                    client = AmazonSQSClientBuilder.standard()
                            .withRegion(getRegions())
                            .withCredentials(new AWSStaticCredentialsProvider(credentials))
                            .withClientConfiguration(config)
                            .build();
                }
            }
        }
    }

    /**
     * キューからメッセージを受信します。
     *
     * @param queueName キュー名
     * @param maxNumberOfMessages 受信されるメッセージの最大数
     * @return 受信結果
     */
    public ReceiveMessageResult receiveMessage(String queueName, int maxNumberOfMessages) {
        String queueUrl = client.getQueueUrl(queueName).getQueueUrl();
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(queueUrl);
        receiveMessageRequest.setMaxNumberOfMessages(maxNumberOfMessages);
        return client.receiveMessage(receiveMessageRequest);
    }

    /**
     * キューからメッセージを削除します。
     *
     * @param queueName キュー名
     * @param receiptHandle 削除するメッセージのreceiptHandle
     * @return 削除結果
     */
    public DeleteMessageResult deleteMessage(String queueName, String receiptHandle) {
        String queueUrl = client.getQueueUrl(queueName).getQueueUrl();
        DeleteMessageRequest deleteMessageRequest = new DeleteMessageRequest(queueUrl, receiptHandle);
        return client.deleteMessage(deleteMessageRequest);
    }
}
